{% comment %}
SPDX-License-Identifier: CC-BY-SA-4.0
SPDX-FileCopyrightText: 2020 seL4 Project a Series of LF Projects, LLC.
{% endcomment %}
{% assign page_url = page.url | split: "/" %}

<div class="sidebar">
{% for item in site.data.sidebar.toc %}
{% assign page_in_list = item.subfolderitems | where: "url", page.url | size %}
{% if page_in_list == 1 or page.url == item.url %}
      <ul class="nav nav-sidebar">
  {% for entry in item.subfolderitems %}
        <li class="{% if page.url == entry.url %}active{% endif %}">
          <a class="" href="{{ entry.url }}">
            {{ entry.page }}
          </a>
        </li>
  {% endfor %}
      </ul>
{% endif %}
{% endfor %}


{% comment %}
The following logic is necessary to unfold the projects dict into an array.
Because the unfolding logic seems to triple the site build time, it is only
executed for production builds. This is all so the project links can be sorted
{% endcomment %}
{% assign projects = site.data.projects %}
{% if jekyll.environment == "production" %}
  {% assign new_projects = "" | split: "." %}
  {% for project_ in site.data.projects %}
    {% assign new_projects = new_projects | push: project_[1] %}
  {% endfor %}
  {% assign projects = new_projects | sort: 'project_order' %}
{% endif %}
{% assign page_in_list = site.data.sidebar.toc[0].subfolderitems | where: "url", page.url | size %}

{% if page_url[1] == "projects" and page_in_list != 1 %}
    <ul class="nav nav-sidebar">
  {% for project in projects %}
    {% assign new_project = "" | split: "." %}
    {% if jekyll.environment != "production" %}
      {% assign new_project = new_project | push: project[1] %}
    {% else %}
      {% assign new_project = new_project | push: project %}
    {% endif %}

    {% assign url = "/projects/" | append: new_project[0].name | append: '/' %}
        <li class="{% if page_url[2] == new_project[0].name %}active{% endif %}">
            <a class="" href="{{url}}">{{new_project[0].display_name}}</a>
        <li>
  {% endfor %}
    </ul>
{% endif %}


{% if page_url[1] == "Tutorials" %}
    <ul class="nav nav-sidebar">
  {% assign tutorials = site.pages | where_exp: 'page', 'page.tutorial' | sort: 'tutorial-order' %}
  {% for t in tutorials %}
        <li class="{% if page.url == t.url %}active{% endif %}">
            <a class="" href="{{t.url}}">{{t.title}}</a>
        <li>
  {% endfor %}
    </ul>
{% endif %}
</div>
